//
//  LGMedicinePlanController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/9.
//

#import "LGMedicinePlanController.h"
#import "LGMedicineSectionHeader.h"
#import "LGMedicinePlanCell.h"
@interface LGMedicinePlanController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGMedicinePlanController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"全部用药方案";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.followup_advice/adviceMedicine" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"medicines"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGMedicinePlanCell *planCell = [tableView dequeueReusableCellWithIdentifier:@"planCell" forIndexPath:indexPath];
    planCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *tempArray = self.dataArray[indexPath.section][@"medicines"];
    if(tempArray.count == 1){
        planCell.type = @"single";
    }else if (indexPath.row == 0){
        planCell.type = @"top";
    }else if (indexPath.row == tempArray.count-1){
        planCell.type = @"bottom";
    }else{
        planCell.type = @"middle";
    }
    planCell.sourceType = LGNSString(self.dataArray[indexPath.section][@"source_type"]);
    planCell.dataDic = tempArray[indexPath.row];
    return planCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGMedicineSectionHeader *headerView = [[LGMedicineSectionHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    headerView.dataDic = self.dataArray[section];
    return headerView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}


#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGMedicinePlanCell class] forCellReuseIdentifier:@"planCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(46);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
